from my_base_sk.MySK import *
from my_base_sk.MyDB import cursor

from semantic_kernel.functions import kernel_function


class DBConnectorPlugin:
    def __init__(self, db_cursor):
        self.db_cursor = db_cursor

    @kernel_function(
        description="查询数据库",  # function 描述
        name="query_database",  # function 名字
    )
    def exec(self, sql_exp: str) -> str:
        self.db_cursor.execute(sql_exp)
        records = cursor.fetchall()
        return str(records)


db_connector = kernel.add_plugin(DBConnectorPlugin(cursor), "DBConnectorPlugin")

result = asyncio.run(
    run_function(
        db_connector["query_database"],
        KernelArguments(sql_exp="SELECT COUNT(*) as count FROM Courses WHERE instructor = '王卓然'"))
)

print(result)
